/**
 * Created by h on 2018/10/22.
 */

$(function(){

    var obj="";
    var nameId=sessionStorage.getItem("nameId")
    console.log(nameId);


    getmenu();
    $(':input').labelauty();




    // 将选中的checkbox添加进obj
    $("input[type~=checkbox]").change(function () {
        if ( this.checked == true){
            obj+=$(this).attr("boxname");
            obj+=",";
        }else{
            debugger;
                obj=","+obj;
                var boxname = ","+$(this).attr("boxname") + ",";
                obj = obj.replace(boxname, ",");
                obj=obj.substr(1);

        }


        var setobj=obj.substring(0,obj.lastIndexOf(','));

        setobj= setobj.split(",");




        var intobj=[];
        setobj.forEach(function(data,index,arr){
            intobj.push(+data);
        });

        setobj=intobj;

   // 排序
        for (var i = 0; i <setobj.length-1; i++) {
            for (var j = i+1; j <setobj.length; j++) {
                if(setobj[i]>setobj[j]){//如果前面的数据比后面的数据大就交换
                    var temp = setobj[i];
                    setobj[i] = setobj[j];
                    setobj[j] = temp;
                }
            }

        }
        var changeobj=[];
        for(var i=0;i<setobj.length;i++){
            changeobj+=setobj[i]+",";
        }

        var changeobj=changeobj.substring(0,obj.lastIndexOf(','));
       console.log(changeobj);

      

        var data={
            user:nameId,
            moduleState:changeobj,
        }

        $.ajax({
            url:'http://localhost:8081/Update_module_data',
            type: 'POST',
            dataType: 'json',
            contentType: "application/json",
            async: false,
            data: JSON.stringify(data),
            xhrFields: {
                withCredentials: true
            },
            success: function (result) {
                console.log("提交成功");
                // setting();
                menu();
                //$(this).next(".navContent").slideToggle(300).siblings(".navContent").slideUp(500);
            }
        });

    });

//checkbox是否选中,状态
var checkboxstat = {
        user:nameId,
}
console.log(checkboxstat);
    $.ajax({

            url:'http://localhost:8081/module_state_data',
            type: 'POST',
            dataType: 'json',
            async: false,
            data: JSON.stringify(checkboxstat),
            contentType: "application/json",
            success: function (result) {
                console.log(result);
                for (var i=0 ;i<result.data.length;i++){
               var ModuleState= result.data[i].ModuleState;
                $("input[boxname='"+ModuleState+"']").prop("checked",true) ;
                obj+=ModuleState+",";
                }
            }
    });



});
//获取菜单的名字铺在页面上
function getmenu() {

    $.ajax({
        url:'http://localhost:8081/set_up_all',
        type: 'POST',
        dataType: 'json',
        contentType: "application/json",
        async: false,

        xhrFields: {
                withCredentials: true
                    },
        success: function (result) {
            console.log(result);
            var get_menu_name="";
            for(var i=0; i<result.data.length;i++){
                get_menu_name+="<div class='bor_divs'>";

                get_menu_name+="<input  type='checkbox' name='checkbox' data-labelauty='"+result.data[i].modeuleName+"' boxname='"+result.data[i].modeule+"'><br/>";
                get_menu_name+="</div>";
            }
            $("#menu_controller").html(get_menu_name);

        }
    });
}





